package com.lhh.company.website.admin;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lhh.company.website.config.SysUserDetails;
import com.lhh.company.website.entity.AboutUs;
import com.lhh.company.website.entity.IndexPic;
import com.lhh.company.website.entity.Res;
import com.lhh.company.website.mapper.IndexPicMapper;
import com.lhh.company.website.service.AboutUsService;
import com.lhh.company.website.service.IndexPicService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Home主页变更
 */
@RestController
public class IndexPageApi{

    @Resource
    AboutUsService aboutUsService;

    @Resource
    IndexPicService IndexPicService;

    @Autowired
    private IndexPicMapper IndexPicMapper;

    /**
     * 获取关于我们
     *
     * @return
     */
    @RequestMapping("/about/get")
    public Res getAboutUs(@RequestParam String language) {
        AboutUs getaboutUs = aboutUsService.getOne(new QueryWrapper<AboutUs>().eq("language", language));
        return Res.succ(getaboutUs);
    };

    @RequestMapping("/about/delete")
    public Res delete(@RequestParam String id) {
        aboutUsService.removeById(id);
        List<AboutUs> getList = aboutUsService.list();
        return Res.succ(getList);
    };

    /**
     * 获取关于列表
     *
     * @return
     */
    @RequestMapping("/about/getlist")
    public Res getAboutUsList() {
        List<AboutUs> getList = aboutUsService.list();
        return Res.succ(getList);
    };

    /**
     * 更新关于我们
     *
     * @return
     */
    @RequestMapping("/about/update")
    public Res updateAboutUs(@RequestBody AboutUs aboutUs ,@AuthenticationPrincipal SysUserDetails sysUserDetails) {

        aboutUs.setCreatorid(sysUserDetails.getId());
        aboutUs.setCreatorname(sysUserDetails.getUsername());

        if(aboutUs.getId()==null || aboutUs.getId().equals("")){
            aboutUsService.save(aboutUs);
        }else{
            aboutUsService.updateById(aboutUs);
        }

        List<AboutUs> getList = aboutUsService.list();
        return Res.succ(getList);
    };

    /**
     * 获取主页图片列表
     * picType 0 轮播图 1产品列表一 2产品列表二
     * @return
     */
    @RequestMapping("/indexpic/getList")
    public Res getList(@RequestBody Map<String,String> map) {
        List<IndexPic> list= IndexPicMapper.getListByPicType(map.get("picType"));
        return Res.succ(list);
    };


    /**
     * 获取详情
     * @param id
     * @return
     */
    @RequestMapping("/indexpic/get")
    public Res get(@RequestParam("id") String id) {
        IndexPic indexPic= IndexPicMapper.getById(id);
        if(indexPic==null){}
        indexPic.setFloatMsgList((List<HashMap<String, String>>) JSONUtils.parse(indexPic.getFloatMsg()));
        indexPic.setFloatMsg(null);
        return Res.succ(indexPic);
    };

    @RequestMapping("/indexpic/save")
    public Res save(@RequestBody IndexPic indexPic, @AuthenticationPrincipal SysUserDetails sysUserDetails) throws IOException {
        IndexPicService.save(indexPic,sysUserDetails);
        return Res.succMsg("保存成功");
    };

    @RequestMapping("/indexpic/update")
    public Res update(@RequestBody IndexPic indexPic, @AuthenticationPrincipal SysUserDetails sysUserDetails) throws IOException {
        IndexPicService.update(indexPic,sysUserDetails);
        return Res.succMsg("更新成功");
    };

    @RequestMapping("/indexpic/delete")
    public Res delete(@RequestBody Map<String,String> map) throws IOException {
        IndexPicService.deleteByid(map.get("id"));
        return Res.succMsg("删除成功");
    };

}
